﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace LANmine
{
    public partial class CreateTeam : Form
    {
        private Language lmLang;
        private User lmUser;
        private Database roDb = new Database("ro");
        private Database rwDb = new Database("rw");

        public CreateTeam(Language lmLang, User lmUser)
        {
            /** Save Language object */
            this.lmLang = lmLang;

            InitializeComponent();
            ApplyLanguage();
        }

        #region Language Specifications
        /**
         * MainForm::ApplyLanguage()
         * ---------------------------
         * Overwrite all text properties of all elements in all forms with the right ones.
         */
        private void ApplyLanguage()
        {
            /** Window Title */
            this.Text = this.lmLang.GetString("compos.btnCreateTeam");

            /** Header */
            lblTeamName.Text = this.lmLang.GetString("cteam.lblTeamName");
            lblCreateTeam.Text = this.lmLang.GetString("compos.btnCreateTeam");

            /** Other controls */
            txtTeamTag.Text = this.lmLang.GetString("cteam.txtTeamTag");
            btnCreateTeam.Text = this.lmLang.GetString("compos.btnCreateTeam");
        }
        #endregion

        #region Member Functions
        /**
         * ComposForm::teamTaken
         * Check if the given team name or tag is taken.
         */
        private bool teamTaken(String tName, String tTag)
        {
            /** Build query string */
            String cQry = "EXEC lmCheckTeam @TeamName = '" + tName.Trim() + "', @TeamTag = '" + tTag.Trim() + "'";

            try {
                /** Check the database for results */
                return this.roDb.checkQuery(cQry);
            }
            catch (Exception e)
            {
                MessageBox.Show(this.lmLang.GetString("cteam.errCheckTeam") + "\n" + e.Message);
                return false;
            }
        }

        #endregion

        #region Event Handlers

        /**
         * Event: CreateTeam::txtTeamName~Leave
         */
        private void txtTeamName_Leave(object sender, EventArgs e)
        {

        }

        /**
         * Event: CreateTeam::txtTeamTag~Enter
         * Occurs when Tag field is focussed.
         */
        private void txtTeamTag_Enter(object sender, EventArgs e)
        {
            /** Clear the box if it has default value */
            if (txtTeamTag.Text == this.lmLang.GetString("cteam.txtTeamTag"))
                txtTeamTag.Clear();
            /** Darken the color */
            txtTeamTag.ForeColor = Color.Black;
        }

        /**
         * Event: CreateTeam::txtTeamTag~Leave
         * Occurs when Tag field loses focus.
         */
        private void txtTeamTag_Leave(object sender, EventArgs e)
        {
            if (txtTeamTag.Text.Length == 0)
            {
                /** Reset the default */
                txtTeamTag.Text = this.lmLang.GetString("cteam.txtTeamTag");
                /** Lighten up the color */
                txtTeamTag.ForeColor = Color.Gray;
            }
        }

        /**
         * Event: CreateTeam::InputCheck
         * Checks the input after every character.
         */
        private void InputCheck(object sender, EventArgs e)
        {
            bool btnState = false;

            /** No empty input */
            if (txtTeamName.Text.Length > 3)
            {
                /** Check if the entered team name isn't taken yet */
                if (teamTaken(txtTeamName.Text, txtTeamTag.Text))
                {
                    /** Team already exists */
                    lblTeamNameErr.Text = this.lmLang.GetString("cteam.errTeam");
                    lblTeamNameErr.ForeColor = Color.Red;
                }
                else
                {
                    /** Team name available */
                    lblTeamNameErr.Text = this.lmLang.GetString("cteam.passTeam");
                    lblTeamNameErr.ForeColor = Color.Green;

                    /** Tag filled in ? */
                    if (txtTeamTag.Text != lmLang.GetString("cteam.txtTeamTag") && txtTeamTag.Text.Length > 0)
                    {
                        /** Check passes and tag is filled in */
                        btnState = true;
                    }
                }

                /** Make the control visible */
                lblTeamNameErr.Visible = true;
            }
            else
            {
                lblTeamNameErr.Visible = false;
            }

            /** Apply changes */
            btnCreateTeam.Enabled = btnState;
        }
        #endregion
    }
}
